package com.zhenai.sys.framework.aspectj.context;

import cn.hutool.core.util.ObjectUtil;
import com.zhenai.bcus.common.base.utils.PhoneUtils;
import com.zhenai.sys.common.utils.StringUtils;
import lombok.*;
import lombok.experimental.SuperBuilder;

import java.util.Arrays;
import java.util.stream.Collectors;


public class VagueQueryUserInfoContext {


    public static final String KEY_TYPE = "keyType";
    public static final String KEY_WORD = "keyword";

    /**
     * 搜索类型 0=全部，1=用户昵称，2=姓名，3=手机号，4=所在地 5 用户id
     *
     * @author 汪焰
     * @date 2024/7/9 19:51
     */
    @Getter
    @AllArgsConstructor
    public enum KeyTypeSql {
        ALL(0, "全部", ""),
        NICKNAME(1, "用户昵称", "um.nickname like concat('%', #{keyword}, '%')"),
        USERNAME(2, "姓名", "um.name like concat('%', #{keyword}, '%')"),
        PHONE(3, "手机号", "um.phone = #{encryptPhone}"),
        CITY(4, "所在地", "um.wxCity like concat('%', #{keyword}, '%')"),
        USER_ID(5, "用户id", "um.id = #{keyword}"),
        ;

        private int keyType;
        private String remark;
        private String sql;

        public static String getSql(Integer keyType, String keyword, String tableAlias) {
            if (ObjectUtil.isEmpty(keyType) || StringUtils.isEmpty(keyword) || StringUtils.isEmpty(tableAlias)) {
                return null;
            }
            KeyTypeSql keyTypeSql = forKey(keyType);
            if (ObjectUtil.isEmpty(keyTypeSql)) {
                return null;
            }
            String sql = KeyTypeSql.ALL != keyTypeSql
                    ? keyTypeSql.getSql()
                    : Arrays.stream(KeyTypeSql.values()).filter(f -> StringUtils.isNotEmpty(f.getSql())).map(m -> m.getSql()).collect(Collectors.joining(" or "));

            tableAlias = tableAlias.contains(".") ? tableAlias : tableAlias.concat(".");

            sql = sql.replaceAll("um.", tableAlias);
            sql = sql.replaceAll("#\\{keyword}", "'".concat(keyword).concat("'"));
            sql = sql.replaceAll("#\\{encryptPhone}", "'".concat(PhoneUtils.encryptPhone(keyword)).concat("'"));
            return StringUtils.buildMessage(" ({}) ", sql);
        }

        public static KeyTypeSql forKey(Integer keyType) {
            if (ObjectUtil.isEmpty(keyType)) {
                return null;
            }
            for (KeyTypeSql value : KeyTypeSql.values()) {
                if (value.getKeyType() == keyType) {
                    return value;
                }
            }
            return null;
        }
    }

    private static final ThreadLocal<VagueQueryData> vagueQuery = new ThreadLocal<>();

    public static VagueQueryData getContext() {
        return vagueQuery.get();
    }

    public static void put(VagueQueryData data) {
        vagueQuery.set(data);
        try {
//            SpringUtils.getBean()
        } catch (Exception e) {

        }
    }

    public static void clearContext() {
        vagueQuery.remove();
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @Getter
    @Setter
    @SuperBuilder
    public static class VagueQueryData {
        // 搜索类型 0=全部，1=用户昵称，2=姓名，3=手机号，4=所在地 5 用户id
        private Integer keyType = 0;
        // 模糊筛选字段
        private String keyword = "";
    }
}
